//--- Projeto Domus
// interactionsource.com
// Koji Pereira, Andre Veloso e Marcos Paulo

/*
 Projeto Domus
 interactionsource.com
 Koji Pereira, Andre Veloso e Marcos Paulo

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/


#include "testApp.h"
//#define _USE_LIVE_VIDEO;

ofVideoGrabber vidGrabber;

//--------------------------------------------------------------
void testApp::setup(){

	vidGrabber.setVerbose(true);
	vidGrabber.setDeviceID(0);
	vidGrabber.initGrabber(320,240);
	//vidGrabber.listDevices();


    colorImg.allocate(320,240);
	grayImage.allocate(320,240);
	grayBg.allocate(320,240);
	grayDiff.allocate(320,240);

	bLearnBakground = true;
	threshold = 80;

	ofSetBackgroundAuto(false);

	isCena01 = false;
	isCalibration = false;


}

//--------------------------------------------------------------
void testApp::update(){
	//ofBackground(100,100,100);

	if (isCena01) {
		ofFill();
		ofEnableAlphaBlending();	// turn on alpha blending
		ofSetColor(0,0,0,10);	    // red, 50% transparent
		ofRect(0,0,1024,768);
	}

    bool bNewFrame = false;

#ifdef _USE_LIVE_VIDEO
	vidGrabber.grabFrame();
	bNewFrame = vidGrabber.isFrameNew();
#else
	vidPlayer.idleMovie();
	bNewFrame = vidPlayer.isFrameNew();
#endif

	if (bNewFrame){

#ifdef _USE_LIVE_VIDEO
		colorImg.setFromPixels(vidGrabber.getPixels(), 320,240);
#else
		colorImg.setFromPixels(vidPlayer.getPixels(), 320,240);
#endif

        grayImage = colorImg;
		if (bLearnBakground == true){
			grayBg = grayImage;		// the = sign copys the pixels from grayImage into grayBg (operator overloading)
			bLearnBakground = false;
		}

		// take the abs value of the difference between background and incoming and then threshold:
		grayDiff.absDiff(grayBg, grayImage);
		grayDiff.threshold(threshold);

		// find contours which are between the size of 20 pixels and 1/3 the w*h pixels.
		// also, find holes is set to true so we will get interior contours as well....
		contourFinder.findContours(grayDiff, 20, (320*240)/3, 10, true);	// find holes
	}

}

//--------------------------------------------------------------
void testApp::draw(){


	if (isCalibration) {

		// draw the incoming, the grayscale, the bg and the thresholded difference, parameters arte position on canvas
		ofSetColor(0xffffff);
		colorImg.draw(20,20);
		grayImage.draw(360,20);
		grayBg.draw(20,280);
	    grayDiff.draw(20,20);

		// then draw the contours: Canvas

		//ofFill();
		//ofSetColor(0x333333);
		//ofRect(360,20,360,240);
		//ofSetColor(0xffffff);

		// we could draw the whole contour finder
		//contourFinder.draw(360,540);

		ofSetColor(0xffffff);
		char reportStr[1024];
		sprintf(reportStr, "bg subtraction and blob detection\npress ' ' to capture bg\nthreshold %i (press: +/-)\nnum blobs found %i, fps: %f", threshold, contourFinder.nBlobs, ofGetFrameRate());
		ofDrawBitmapString(reportStr, 20, 600);
	}

	if (isCena01) {
		// or, instead we can draw each blob individually,
		// this is how to get access to them:
		ofFill();
		ofSetColor(255,255,255);
		ofSetPolyMode(OF_POLY_WINDING_NEGATIVE);
		ofBeginShape();

		for (int i = 0; i < contourFinder.nBlobs; i++){
			//       contourFinder.blobs[i].draw(360,20);

			for (int j = 0; j < contourFinder.blobs[i].nPts; j++) {
				ofVertex(contourFinder.blobs[i].pts[j].x*3.2, contourFinder.blobs[i].pts[j].y*3.2);
				//ofLine(contourFinder.blobs[i].pts[j].x, contourFinder.blobs[i].pts[j].y,contourFinder.blobs[i].pts[j+1].x, contourFinder.blobs[i].pts[j+1].y);
			}
		}
		ofEndShape();
	}

}


//--------------------------------------------------------------
void testApp::keyPressed  (int key){

	switch (key){
		case ' ':
			bLearnBakground = true;
			break;
		case '+':
			threshold ++;
			if (threshold > 255) threshold = 255;
			break;
		case '-':
			threshold --;
			if (threshold < 0) threshold = 0;
			break;
		case '1':
			ofSetFrameRate(60);
			isCena01 = true;
			isCalibration = false;
			break;
		case 'c':
			//Enter calibration
			isCalibration = true;
			isCena01 = false;
			break;

	}
}

//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){
}

//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){
}

//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
}

//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button){

}

//--------------------------------------------------------------
void testApp::windowResized(int w, int h){

}

